//
//  PEDeviceStatusCircleView.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/10/12.
//

#import "RHDeviceStatusCircleView.h"
@interface RHDeviceStatusCircleView()

@property (nonatomic, assign) CGFloat normalPercent;
@property (nonatomic, assign) CGFloat offlinePercent;
@property (nonatomic, assign) CGFloat alarmPercent;
@property (nonatomic, assign) CGFloat faultPercent;
@end
@implementation RHDeviceStatusCircleView
-(instancetype)init
{
    if(self = [super init]){
        [self setupUI];
    }
    return self;
}
-(void)setupUI{
    
    self.backgroundColor = [UIColor whiteColor];
   
}
-(void)layoutSubviews
{
    [self setNeedsDisplay];
}

-(void)setPercent:(CGFloat)percent
{
    _percent = percent;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    
    //正常
    CGPoint center = CGPointMake(self.width/2, self.height/2);
    CGFloat  normalLineWidth = 4;
    CGFloat  normalRadius = MIN(self.width, self.height)/2 - normalLineWidth;
    //灰色圆环
    UIBezierPath *bgPath = [UIBezierPath bezierPathWithArcCenter:center radius:normalRadius startAngle:0 endAngle:M_PI*2 clockwise:NO];
    bgPath.lineWidth = normalLineWidth-2;
    [kCOLOR_HEX_A(@"000000", 0.08) setStroke];
    [bgPath stroke];
    
    
    CGFloat startAngle = self.startAngle;
    CGFloat perc = (1.0 - self.percent);
    if (self.percent == 0) {
        perc = 0;
    }
    if (self.percent >= 1.0) {//百分之百的时候
        perc = 0.0001;
    }
    CGFloat endAngle = M_PI * 2 * perc + startAngle;
    //进度
    UIBezierPath *percentPath = [UIBezierPath bezierPathWithArcCenter:center radius:normalRadius startAngle:startAngle endAngle:endAngle clockwise:NO];
    percentPath.lineWidth = normalLineWidth;
    percentPath.lineCapStyle = kCGLineCapRound;
    [kCOLOR_HEX(self.lineColor) setStroke];
    [percentPath stroke];
    
    
    //小圆圈背景
    if (perc!=0) {
        CGFloat x = cosf(self.startAngle)*normalRadius + center.x;
        CGFloat y = sinf(self.startAngle)*normalRadius + center.y;
        
        UIBezierPath *litleCircle_bg = [UIBezierPath bezierPathWithArcCenter:CGPointMake(x, y) radius:3 startAngle:0 endAngle:M_PI*2 clockwise:YES];
        [kCOLOR_HEX(@"ffffff") setFill];
        [litleCircle_bg fill];
        
        //小圆圈
        UIBezierPath *litleCircle = [UIBezierPath bezierPathWithArcCenter:CGPointMake(x, y) radius:3 startAngle:0 endAngle:M_PI*2 clockwise:YES];
        [kCOLOR_HEX(self.lineColor) setStroke];
        litleCircle.lineWidth = 1;
        [litleCircle stroke];
    }
 
}

@end
